package web.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import web.entity.LoginUser;
import web.entity.User;
import web.service.PermissionSerive;
import web.service.UserService;

import java.util.List;
import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserService userService;

    @Autowired
    private PermissionSerive permissionSerive;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        User user = userService.selectOne(username);
        if (Objects.isNull(user)) {
            throw new RuntimeException("用户名不存在");
        }

        List<String> persByID = permissionSerive.getPersByID(user.getId().toString());

        return new LoginUser(user, persByID);
    }

}
